package com.hub.controller.asset;

import com.hub.controller.BaseController;
import com.hub.dto.asset.RegionInfoDto;
import com.hub.dto.asset.SearchRegionInfoDto;
import com.hub.service.asset.IRegionInfoService;
import com.hub.utils.ApiResultObject;
import com.hub.utils.PagingResultObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author hub
 */
@RestController
@RequestMapping(value = "/region")
public class RegionInfoController extends BaseController {
    @Autowired
    private IRegionInfoService regionInfoService;

    @ApiOperation("分页查询区域列表")
    @PostMapping(value = "/list")
    public PagingResultObject getlist(@RequestBody SearchRegionInfoDto queryModel){
        return this.regionInfoService.getList(queryModel);
    }

    @ApiOperation("新增区域")
    @PostMapping(value = "/add")
    public ApiResultObject addRegion(@RequestBody @Valid RegionInfoDto addModel, BindingResult bindingResult){
        //数据验证
        ApiResultObject validResult = this.checkValidResult(bindingResult);
        if(!validResult.isSuccess()){
            return validResult;
        }
        return this.regionInfoService.addRgion(addModel);
    }

    @ApiOperation("修改区域")
    @PutMapping(value = "/{id}")
    public ApiResultObject modifyRegion(@RequestBody @Valid RegionInfoDto modifyModel, BindingResult bindingResult){
        //数据验证
        ApiResultObject validResult = this.checkValidResult(bindingResult);
        if(!validResult.isSuccess()){
            return validResult;
        }
        return this.regionInfoService.modifyRegion(modifyModel);
    }

    @ApiOperation("删除区域")
    @DeleteMapping(value = "/{id}")
    public ApiResultObject deleteRegion(@PathVariable int id){
        return this.regionInfoService.deleteRegion(id);
    }
}
